// pages/home/collection/collection-more/index.js
const app = getApp();
const {
  AskPage
} = app._require("common/index");
const formMixin = app._require("mixins/form.mixin.js").default;
AskPage({

  name: "collection-more",
  mixins: [formMixin],
  data: {
    list: []
  },
  onLoad (options) {
    var departmentId = options.departmentId;
    console.log(options.departmentId);
    this.initData(departmentId);
  },

  listItemTap(e) {
    console.log("详情");
    let parentindex = e.currentTarget.dataset.parentindex, //点击的内层所在的最外层列表下标
      Index = e.currentTarget.dataset.index, //点击的内层下标
      list = this.data.list;
    console.log(list[parentindex].item, Index,list[parentindex].item[Index]);
    this.setData({
      list
    });
  },
  share(e){
    console.log("分享");
    let parentindex = e.currentTarget.dataset.parentindex, //点击的内层所在的最外层列表下标
    Index = e.currentTarget.dataset.index, //点击的内层下标
    list = this.data.list;
    console.log(list[parentindex].item, Index,list[parentindex].item[Index]);
    this.setData({
      list
    });
  },

  async initData(departmentId) {
    try{
    
      const response = await this.$_server("collection/getDeptTemplate", {
        data: {
          departmentId:departmentId
        },
      });
      let { data } = response;
      if (data.code !== 200) {
          throw data;
      }
      // console.log('---');
       console.log(data.data);
       let list = [];
      for (const key in data.data) {
          const element = data.data[key];
          list.push({
            listName: key.replace('\"', '').replace('\"', ''),
            item: element.map(cur=>{
              return{
                id: cur.id || "",
                departmentId: cur.departmentId || "",
                itemName: cur.formName || ""
              }
            })
          })
      }
      this.setData({list:list})
    }catch (error) {
      console.log(error);
      if (error.message) {
        wx.showToast({
            mask: true,
            title: error.message,
            icon: "none",
            duration: 2000,
        });
    } else {
        throw error;
    }
    }
  }


});
